package com.emuporium.gymrat.server.log;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.emuporium.gymrat.server.data.Exercise;
import com.emuporium.gymrat.server.data.ExerciseDB;
import com.emuporium.gymrat.server.data.LogDB;
import com.emuporium.gymrat.server.data.LogEntry;
import com.emuporium.gymrat.server.data.Routine;
import com.emuporium.gymrat.server.data.RoutineDB;
import com.emuporium.gymrat.server.data.User;
import com.emuporium.gymrat.server.data.UserDB;

@SuppressWarnings("serial")
public class LogViewServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		User user = UserDB.selectUser(request.getRemoteUser());

		ArrayList<LogEntry> log = new ArrayList<LogEntry>();
		if (request.getAttribute("log") == null) {
			log = LogDB.selectLogEntries(user.getId());
			request.setAttribute("log", log);
		}

		ArrayList<Routine> routines = RoutineDB.getRoutines(user.getEmail());
		ArrayList<Exercise> exercises = ExerciseDB.searchExercises(
				user.getId(), -1, false);

		request.setAttribute("routines", routines);
		request.setAttribute("exercises", exercises);

		String url = "/log/viewLog.jsp";
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(url);
		dispatcher.forward(request, response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
